package com.zp.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.paper.AnswerItemVo;
import com.zp.bean.paper.ResultAnswerItemVo;
import com.zp.domain.AnswerItem;
import com.zp.domain.Question;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * answer item services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/22
 * 
 */
@Component
@Path("/rest/aitem")
@Singleton
public class AnswerItemServices extends BaseServices {

	/**
	 * add one AnswerItem path:/aitem/addItem Http:POST
	 * 
	 * @param aitem
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addItem")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addItem(AnswerItem aitem) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(aitem);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update one AnswerItem path:/aitem/updateItem Http:PUT
	 * 
	 * @param aitem
	 * 
	 * @return Response, ResultAnswerItemVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"vo":{"id":
	 *         "402881e6445d4c3701445d515f8e0002"
	 *         ,"code":"A","question":{"id":"402881e6445cc3a301445cc4332b0001"
	 *         ,"title":"心理关爱题目1","questionType":"QUS00001","questionAnswer":"",
	 *         "businessType":null},"value":"心理关爱题目1答案1"},"list":null}
	 */
	@PUT
	@Path("/updateItem")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateItem(AnswerItem aitem) {
		ResultAnswerItemVo result = new ResultAnswerItemVo();
		try {
			jpaDao.update(aitem);
			AnswerItemVo vo = new AnswerItemVo();
			vo.setId(aitem.getId());
			vo.setCode(aitem.getAnswerCode());
			vo.setQuestion((Question) this.jpaDao.findById(Question.class,
					aitem.getQuestionId()));
			vo.setValue(aitem.getValue());
			result.setVo(vo);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete AnswerItem by id path:/aitem/deleteItem Http:DELETE
	 * 
	 * @param itemId
	 *            primary key of AnswerItem
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deleteItem")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteItem(@QueryParam("itemId") String itemId) {
		logger.info("deleteItem called itemId:" + itemId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(AnswerItem.class, itemId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get AnswerItem by id path:/aitem/getItem Http:GET
	 * 
	 * @param itemId
	 *            primary key of AnswerItem
	 * 
	 * @return Response, ResultAnswerItemVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"vo":{"id":
	 *         "402881e6445d4c3701445d515f8e0002"
	 *         ,"code":"A","question":{"id":"402881e6445cc3a301445cc4332b0001"
	 *         ,"title":"心理关爱题目1","questionType":"QUS00001","questionAnswer":"",
	 *         "businessType":null},"value":"心理关爱题目1答案1"},"list":null}
	 * 
	 */
	@GET
	@Path("/getItem")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getItemById(@QueryParam("itemId") String itemId) {
		logger.info("getItemById called itemId:" + itemId);
		ResultAnswerItemVo result = new ResultAnswerItemVo();

		AnswerItem aitem;
		try {
			aitem = (AnswerItem) jpaDao.findById(AnswerItem.class, itemId);
			AnswerItemVo vo = new AnswerItemVo();
			vo.setId(aitem.getId());
			vo.setCode(aitem.getAnswerCode());
			try {
				vo.setQuestion((Question) this.jpaDao.findById(Question.class,
						aitem.getQuestionId()));
			} catch (IllegalArgumentException e) {
				vo.setQuestion(null);
			}
			vo.setValue(aitem.getValue());
			result.setVo(vo);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();

		}
		return Response.ok(result).build();
	}

	/**
	 * 
	 * get answer items with page parameters and search condition
	 * path:/aitem/getItems Http:GET
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param value
	 *            the value of answer,if ignored the value is null
	 * 
	 * @return Response, ResultAnswerItemVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":12,"vo":null,"list":[
	 *         {"id":"402881e6445d53b401445d54be830001"
	 *         ,"code":"B","question":{"id"
	 *         :"402881e6445cc3a301445cc4332b0001","title"
	 *         :"心理关爱题目1","questionType"
	 *         :"QUS00001","questionAnswer":"","businessType"
	 *         :null},"value":"心理关爱题目1答案2"
	 *         },{"id":"402881e6445d53b401445d54fa180002"
	 *         ,"code":"C","question":{
	 *         "id":"402881e6445cc3a301445cc4332b0001","title"
	 *         :"心理关爱题目1","questionType"
	 *         :"QUS00001","questionAnswer":"","businessType"
	 *         :null},"value":"心理关爱题目1答案3"}]}
	 * 
	 * 
	 */
	@GET
	@Path("/getItems")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getItemByCodeName(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("value") String value) {
		logger.info("getItemByCodeName called json pageNo:" + pageNo
				+ " pageSize:" + pageSize + " value:" + value);
		String jpql = "from AnswerItem where value=? order by questionId asc";
		String countSql = "select count(id) from AnswerItem where value=? order by questionId asc";
		List<?> list = null;
		ResultAnswerItemVo result = new ResultAnswerItemVo();
		List<AnswerItemVo> lt = new ArrayList<AnswerItemVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(value) || value == null) {
			jpql = "from AnswerItem order by questionId asc";
			countSql = "select count(id) from AnswerItem order by questionId asc";
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, null);
				long totalCount = this.jpaDao.getCount(countSql, null);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] param = { value };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
				long totalCount = this.jpaDao.getCount(countSql, param);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				AnswerItem aitem = (AnswerItem) o;
				AnswerItemVo vo = new AnswerItemVo();
				vo.setId(aitem.getId());
				vo.setCode(aitem.getAnswerCode());
				try {
					vo.setQuestion((Question) this.jpaDao.findById(
							Question.class, aitem.getQuestionId()));
				} catch (MyException e) {
					vo.setQuestion(null);
					e.printStackTrace();
				}
				vo.setValue(aitem.getValue());
				lt.add(vo);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}
}
